package com.example.helpconnect;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.controller.*;
import com.example.model.*;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GoogleMapsActivity extends MapActivity{      
    private MapView mapView;
    
    Button searchBtn;
    
    private static final int latitudeE6 = 37985339;
    private static final int longitudeE6 = 23716735;
    Drawable drawable;
    GoogleMapsOverlay itemizedOverlay;
    
    User user;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        Intent i = getIntent();
		i.getStringExtra("key");
		int userID = i.getIntExtra("userID", -1);
		user = new User(new UserCRUD());
		
		user = user.read(userID);
		int latitude =(int)(user.getLatitude() * (Math.pow(10, 6)));
		int longitude = (int)(user.getLongitude() * (Math.pow(10, 6)));
		
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        
        drawable = this.getResources().getDrawable(R.drawable.marker);
        itemizedOverlay = new GoogleMapsOverlay(drawable, this);
        
        searchBtn =  (Button)findViewById(R.id.searchMapBtn);
        searchBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) { onClickSearch(); }

			private void onClickSearch() {
				EditText searchStringField = (EditText)findViewById(R.id.mapSearchField);
				
				String searchString = searchStringField.getText().toString();
				
				Service utilityService = new Service(new ServiceCRUD());
				ArrayList<Service> searchedServices = (ArrayList<Service>) utilityService.readAllByKeywords(searchString, user.getUserID());
				
				plotServicesOnMap(searchedServices);
				
				
			}

			
			
        });
        
    }
    
    public void plotServicesOnMap(List<Service> services){
    	
		List<Overlay> mapOverlays = mapView.getOverlays();
		
		mapOverlays.clear();
		
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new GoogleMapsOverlay(drawable, this);
        
        GeoPoint point = new GeoPoint((int)(user.getLatitude() * (Math.pow(10, 6))), (int)(user.getLongitude() * (Math.pow(10, 6))));
        OverlayItem overlayitem = new OverlayItem(point, user.getFirstName() + " " + user.getLastName(), "You are here!");
        itemizedOverlay.addOverlay(overlayitem);
        
        //create overlays for each services
        for(int i = 0; i<services.size(); i++){
        	Service currentService = services.get(i);
        	User currentServiceOwner = user.read(services.get(i).getUserID());
        	
        	GeoPoint servicePoint = new GeoPoint((int)(currentServiceOwner.getLatitude() * (Math.pow(10, 6))), (int)(currentServiceOwner.getLongitude() * (Math.pow(10, 6))));
        	double costOfService = currentService.getCost();
        	NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);
        	String cost = fmt.format(costOfService);
        	String overlayTitle = currentServiceOwner.getFirstName() + " " + currentServiceOwner.getLastName();
        	
        	//The service code in the snippet of the overlay represents (ServiceID-ServiceOwnerID-RequesterUserID). 
        	//This allows us to send these parameters to the overlay in order to create the service request in
        	//in the backend.
        	String overlaySnippet = currentService.getDescription()+"\nCost of Service: " + cost + "\n\nservice code: " +
        			Integer.toString(currentService.getServiceID()) + "-" + Integer.toString(currentService.getUserID()) + "-" + Integer.toString(user.getUserID());
            OverlayItem serviceOverlayitem = new OverlayItem(servicePoint, overlayTitle, overlaySnippet);
            itemizedOverlay.addOverlay(serviceOverlayitem);
        	
        }
       
        mapOverlays.add(itemizedOverlay);
        
        MapController mapController = mapView.getController();

        mapController.animateTo(point);
        mapController.setZoom(16);
	}
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}
